package com.finger.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.eduservice.entity.EduComment;
import com.finger.eduservice.mapper.EduCommentMapper;
import com.finger.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author finger
 * @since 2022-01-26
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Override
    public Map<String, Object> pageComment(Page<EduComment> eduCommentPage, String id) {

        QueryWrapper<EduComment> eduCommentQueryWrapper = new QueryWrapper<>();

        eduCommentQueryWrapper.eq("course_id",id);

        eduCommentQueryWrapper.orderByDesc("gmt_create");

        IPage<EduComment> page = baseMapper.selectPage(eduCommentPage, eduCommentQueryWrapper);

        long total = page.getTotal();

        List<EduComment> records = page.getRecords();

        long size = page.getSize();

        long pages = page.getPages();

        long current = page.getCurrent();

        boolean hasNext =true;
        boolean hasPrevious = true;

        if(current==pages){
            hasNext = false;
        }else if(current ==1){
            hasPrevious = false;
        }

        HashMap<String, Object> map = new HashMap<>();

        map.put("total",total);
        map.put("items",records);
        map.put("size",size);
        map.put("pages",pages);
        map.put("current",current);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }
}
